import * as fs from 'fs';
import * as path from 'path';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './modules/auth/auth.module';
import { UserModule } from './modules/user/user.module';

const { ACTIVE } = process.env;

const tidb = TypeOrmModule.forRoot({
    type: 'mysql',
    host: process.env.TIDB_HOST,
    port: +process.env.TIDB_PORT,
    username: process.env.TIDB_USER,
    password: process.env.TIDB_PASSWORD,
    database: process.env.TIDB_DATABASE,
    entities: [__dirname + '/**/*.entity{.ts,.js}'],
    synchronize: true,
    ssl: fs.readFileSync(path.join(process.cwd(), process.env.TIDB_CA_PATH)),
    logging: true,
})

const pgsql = TypeOrmModule.forRoot({
    type: "postgres",
    host: process.env.PG_HOST,
    port: Number(process.env.PG_PORT) || 5432,
    username: process.env.PG_USER,
    password: process.env.PG_PASSWORD,
    database: process.env.PG_DATABASE,
    entities: [__dirname + '/**/*.entity{.ts,.js}'],
    synchronize: true,
    logging: true
})

export const imports = [
    ACTIVE == "dev" ? tidb : ACTIVE == "pro" ? pgsql : tidb,
    AuthModule,
    UserModule,
];
